# 迭代器使用
import collections
import time


class MyList(object):
    def __init__(self):
        self.list1 = list()
        # 定义一个迭代数据的下标
        self.index = 0

    def add(self, name):
        self.list1.append(name)

    # 想要某一个类有迭代（Iterable）功能必须有__iter__方法
    def __iter__(self):
        # 返回的类必须要有__iter__方法和__next__方法才可以真正迭代
        return self

    def __next__(self):
        time.sleep(1)
        if self.index < len(self.list1):
            # 迭代的数据
            data = self.list1[self.index]
            # 迭代之后将下标自增1
            self.index += 1
            return data
        else:
            # 抛出此异常迭代器就终止
            raise StopIteration


if __name__ == "__main__":
    myList = MyList()
    # myList.add("zhangsan")
    # myList.add("lisi")
    # myList.add("wangwu")
    # myList.add("zhaoliu")
    #
    # for name in myList:
    #     print(name)

    print(isinstance(myList, collections.Iterable))
    print(isinstance(myList, collections.Iterator))
